package vn.cuhanh.models;

import java.util.Calendar;

import vn.cuhanh.R;
import vn.cuhanh.sys.DBAdapter;
import vn.cuhanh.utils.HelperFunc;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;

public class Model_Minigame {
	
	public static final int DEFAULT_SCORE = 5;
	
	public static boolean isDailyClickAvailable(Context ctx, String userName) {
		DBAdapter dbAdapter;
		Cursor cursor = null;
		dbAdapter = new DBAdapter(ctx, ctx.getResources().getString(
				R.string.dailyDndScoreTable));
		try {
			if (dbAdapter.isClosed()) {
				dbAdapter.open();
			}
			cursor = dbAdapter.fetch("userName = '" + userName + "'");

			return isDailyClickAvailable(cursor);
		} catch (SQLException e) {
			HelperFunc.readStackTrace(e);
		} finally {
			try {
				if (cursor != null) {
					cursor.close();
				}
				dbAdapter.close();
			} catch (Exception e) {
				HelperFunc.readStackTrace(e);
			}
		}
		return true;
	}
	
	private static boolean isDailyClickAvailable(Cursor cursor) {
		cursor.moveToFirst();
		if(cursor.getCount() < 1 || cursor.isAfterLast()) {
			return true;
		}
		
		long lastClickSecond = cursor.getLong(cursor.getColumnIndexOrThrow("lastClick"));
		Calendar lastClick = Calendar.getInstance();
		lastClick.setTimeInMillis(lastClickSecond * 1000);
		Calendar current = Calendar.getInstance();
		if(lastClick.get(Calendar.YEAR) < current.get(Calendar.YEAR)) {
			return true;
		}
		if(lastClick.get(Calendar.YEAR) == current.get(Calendar.YEAR) && lastClick.get(Calendar.DAY_OF_YEAR) < current.get(Calendar.DAY_OF_YEAR)) {
			return true;
		}
		return false;
	}
	
	public static int getDailyScore(Context ctx, String userName) {
		DBAdapter dbAdapter;
		Cursor cursor = null;
		dbAdapter = new DBAdapter(ctx, ctx.getResources().getString(
				R.string.dailyDndScoreTable));
		try {
			if (dbAdapter.isClosed()) {
				dbAdapter.open();
			}
			cursor = dbAdapter.fetch("userName = '" + userName + "'");

			return getDailyScore(cursor);
		} catch (SQLException e) {
			HelperFunc.readStackTrace(e);
		} finally {
			try {
				if (cursor != null) {
					cursor.close();
				}
				dbAdapter.close();
			} catch (Exception e) {
				HelperFunc.readStackTrace(e);
			}
		}
		return DEFAULT_SCORE;
	}
	
	private static int getDailyScore(Cursor cursor) {
		cursor.moveToFirst();
		if(cursor.getCount() < 1 || cursor.isAfterLast()) {
			return DEFAULT_SCORE;
		}
		
		return cursor.getInt(cursor.getColumnIndexOrThrow("score"));
	}
	
	public static boolean saveDailyScore(Context ctx, int lastClick, int score, String userName) {
		DBAdapter dbAdapter = new DBAdapter(ctx, ctx.getResources().getString(R.string.dailyDndScoreTable));
		try {
			if(dbAdapter.isClosed()) {
				dbAdapter.open();
			}
			ContentValues values = new ContentValues();
			values.put("score", score);
			dbAdapter.update(values, "1 = 1");
			values.put("userName", userName);
			values.put("lastClick", lastClick);
			if(dbAdapter.insert(values) == -1) {
				dbAdapter.update(values, "userName = '" + userName + "'");
			}
		} catch (SQLException e) {
			// TODO: handle exception
			HelperFunc.readStackTrace(e);
			return false;
		} finally {
			dbAdapter.close();
		}
		return true;
	}
}
